package com.infodms.dms.actions.sysusermng.sysuserinfo;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.infodms.dms.bean.AclUserBean;
import com.infodms.dms.common.Constant;
import com.infodms.dms.common.ErrorCodeConstant;
import com.infodms.dms.common.MD5Util;
import com.infodms.dms.exception.BizException;
import com.infodms.dms.po.TcUserPO;
import com.infodms.dms.po.TcUserPasswordPO;
import com.infodms.dms.util.ActionUtil;
import com.infodms.dms.util.CommonUtils;
import com.infoservice.mvc.context.ActionContext;
import com.infoservice.mvc.context.RequestWrapper;
import com.infoservice.po3.POFactory;
import com.infoservice.po3.POFactoryBuilder;

public class SysPasswordManager {
	public Logger logger = Logger.getLogger(SysPasswordManager.class);
	private POFactory factory = POFactoryBuilder.getInstance();
	private ActionContext act = ActionContext.getContext();
	private AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
	
	private final String searchUserInfoURL = "/jsp/sysusermng/SearchUserPasswordModefyInfo.jsp";
	
	/** 
	 * Function       :  密码维护初始化
	 * @throws        :  Exception
	 * update		  :  修改密码的读写方式
	 * LastUpdate     :  2010-5-28
	 */
	public void queryUserHistoryPasswordInfoInit(){
		try {	
			//得到当前登陆用户名
			Long userId = logonUser.getUserId();
			//得到当前登陆用户的基本信息
			TcUserPO user = new TcUserPO();
			user.setUserId(userId);
			user = factory.select(user).get(0);
			act.setOutData("user", user);
			
			/*TcUserPasswordPO password = new TcUserPasswordPO();
			password.setUserId(userId);
			List<TcUserPasswordPO> passList = factory.select(password);
			if(passList.size() > 0){
				password = passList.get(0);
			}
			act.setOutData("password", password);*/			
			
			act.setForword(searchUserInfoURL);
		}catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"密码维护初始化");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	
	/**
	 * Function       :  密码修改
	 * @throws        :  Exception
	 * LastUpdate     :  2010-1-12
	 */
	public void modefyPasswordInfo(){
		try {	
			RequestWrapper request = act.getRequest();
			
			//得到页面参数
			String userId = CommonUtils.checkNull(request.getParamValue("userId"));
			String password = CommonUtils.checkNull(request.getParamValue("passId"));
			//现在用的密码
			String nowPassword = CommonUtils.checkNull(request.getParamValue("nowPassword"));
			//新密码
			String newPassword = CommonUtils.checkNull(request.getParamValue("newPassword"));
						
			if(null != password && !"".equals(password)){
				//验证密码是否相同
				Boolean checkStatus = false;
				//checkStatus = MD5Util.checkPassword(nowPassword, new Long(userId));
				checkStatus = (MD5Util.MD5Encryption(nowPassword)).equals(password);
				if(checkStatus){
					
					TcUserPO poDB = new TcUserPO();
					poDB.setUserId(new Long(userId));
					TcUserPO poValue = new TcUserPO();
					poValue.setPassword(MD5Util.MD5Encryption(newPassword));
					poValue.setUpdateBy(new Long(userId));
					poValue.setUpdateDate(new Date(System.currentTimeMillis()));
					factory.update(poDB, poValue);	
					
					StringBuffer checkMessage = new StringBuffer();
					checkMessage.append("密码修改成功");
					act.setOutData("checkMessage", checkMessage);		
				}else {
					StringBuffer checkMessage = new StringBuffer();
					checkMessage.append("密码错误，请重新输入");
					act.setOutData("checkMessage", checkMessage);
				}
			}else{
				//如果此用户没有创建过密码，则在此新建
				
				TcUserPO tp = new TcUserPO();
				tp.setUserId(new Long(userId));
				TcUserPO po = new TcUserPO();
				po.setPassword(MD5Util.MD5Encryption(newPassword));
				po.setUpdateBy(new Long(userId));
				po.setUpdateDate(new Date(System.currentTimeMillis()));
				
				factory.update(tp, po);
				
				StringBuffer checkMessage = new StringBuffer();
				checkMessage.append("密码创建成功");
				act.setOutData("checkMessage", checkMessage);
			}
			
			//得到当前登陆用户的基本信息
			TcUserPO user = new TcUserPO();
			user.setUserId(new Long(userId));
			user = factory.select(user).get(0);
			act.setOutData("user", user);
			
			act.setForword(searchUserInfoURL);
			
		}catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"个人信息查询");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	
}
